home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / contrib / amiga / AmigaSrc / UUCP-1.15D / popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-05  |  3.0 KB  |  142 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "popen.h"
  4. #include <string.h>
  5. #include "config.h"
  6.  
  7. Prototype FILE *popen(char *command, char *mode);
  8. Prototype int pclose(FILE * current);
  9.  
  10. #ifndef AMIGA
  11. static char template[] = "piXXXXXX";
  12. #else
  13. static void mkcmd(char *, char *, char, char *);
  14. extern char *mktemp(char *);
  15. #endif
  16.  
  17. #ifdef _DCC
  18. #define _NFILE FOPEN_MAX
  19. #endif
  20.  
  21. typedef enum { unopened = 0, reading, writing } pipemode;
  22. static
  23. struct {
  24.     char *command;
  25.     char *name;
  26.     pipemode pmode;
  27. } pipes[_NFILE];
  28.  
  29. FILE *
  30. popen( char *command, char *mode ) {
  31.     FILE *current;
  32.     char *name;
  33.     int cur;
  34.     pipemode curmode;
  35. #ifdef AMIGA
  36. char *template = "T:piXXXXXX";
  37. #endif
  38.     /*
  39.     ** decide on mode.
  40.     */
  41.     if(strcmp(mode,"r") == 0)
  42.         curmode = reading;
  43.     else if(strcmp(mode,"w") == 0)
  44.         curmode = writing;
  45.     else
  46.         return NULL;
  47.     /*
  48.     ** get a name to use.
  49.     */
  50. #ifdef AMIGA
  51.     name = mktemp(template);
  52. #else
  53.     if((name = tempnam(".","pip"))==NULL)
  54.         return NULL;
  55. #endif
  56.     /*
  57.     ** If we're reading, just call system to get a file filled with
  58.     ** output.
  59.     */
  60.     if(curmode == reading) {
  61.         char cmd[256];
  62. #ifdef AMIGA
  63.         mkcmd(cmd, command, '>', name);
  64. #else
  65.         sprintf(cmd,"%s > %s",command,name);
  66. #endif
  67.         system(cmd);
  68.         if((current = fopen(name,"r")) == NULL)
  69.             return NULL;
  70.     } else {
  71.         if((current = fopen(name,"w")) == NULL)
  72.             return NULL;
  73.     }
  74.     cur = fileno(current);
  75.     pipes[cur].name = strdup(name);
  76.     pipes[cur].pmode = curmode;
  77.     pipes[cur].command = strdup(command);
  78.     return current;
  79. }
  80.  
  81. int
  82. pclose( FILE * current) {
  83.     int cur = fileno(current),rval;
  84.     /*
  85.     ** check for an open file.
  86.     */
  87.     if(pipes[cur].pmode == unopened)
  88.         return -1;
  89.     if(pipes[cur].pmode == reading) {
  90.         /*
  91.         ** input pipes are just files we're done with.
  92.         */
  93.         rval = fclose(current);
  94.         unlink(pipes[cur].name);
  95.     } else {
  96.         /*
  97.         ** output pipes are temporary files we have
  98.         ** to cram down the throats of programs.
  99.         */
  100.         char command[256];
  101.         fclose(current);
  102. #ifdef AMIGA
  103.         mkcmd(command, pipes[cur].command, '<', pipes[cur].name);
  104. #else
  105.         sprintf(command,"%s < %s",pipes[cur].command,pipes[cur].name);
  106. #endif
  107.         rval = system(command);
  108.         unlink(pipes[cur].name);
  109.     }
  110.     /*
  111.     ** clean up current pipe.
  112.     */
  113.     pipes[cur].pmode = unopened;
  114.     free(pipes[cur].name);
  115.     free(pipes[cur].command);
  116.     return rval;
  117. }
  118. #ifdef AMIGA
  119. /* ^%#@%! Amiga CLI requires I/O redirection immediately after command name.
  120.    Assume command name ends at first space and place redirection there */
  121.  
  122. static void
  123. mkcmd(char *cmd, char *command, char op, char *name)
  124. {
  125.   while (*command == ' ') {
  126.     command++;
  127.   }
  128.   while (*command != ' ' && *command != '\000') {
  129.     *cmd++ = *command++;
  130.   }
  131.   *cmd++ = ' ';
  132.   *cmd++ = op;
  133.   strcpy(cmd, name);
  134.   cmd += strlen(name);
  135.   while (*command != '\000') {
  136.     *cmd++ = *command++;
  137.   }
  138.   *cmd = '\000';
  139.   return;
  140. }
  141. #endif
  142.